<?php
App::uses('AppController', 'Controller');
App::uses('IMDbScraper','Lib/WebService');
/**
 * Movies Controller
 *
 * @property Movie $Movie
 */
class MoviesController extends AppController {

	public $helpers = array('Form', 'Html');
	public $paginate = array('limit' => 5);
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Movie->recursive = 0;
		$this->set('movies', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		$this->set('movie', $this->Movie->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Movie->create();
			if ($this->Movie->save($this->request->data)) {
				$this->Session->setFlash(__('The movie has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The movie could not be saved. Please, try again.'));
			}
		}
		$actors = $this->Movie->Actor->find('list');
		$directors = $this->Movie->Director->find('list');
		$keywords = $this->Movie->Keyword->find('list');
		$movies = $this->Movie->Movies->find('list');
		$this->set(compact('actors', 'directors', 'keywords','movies'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Movie->save($this->request->data)) {
				$this->Session->setFlash(__('The movie has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The movie could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Movie->read(null, $id);
		}
		$actors = $this->Movie->Actor->find('list');
		$directors = $this->Movie->Director->find('list');
		$keywords = $this->Movie->Keyword->find('list');
		$movies = $this->Movie->Movies->find('list');
		$this->set(compact('actors', 'directors', 'keywords','movies'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Movie->id = $id;
		if (!$this->Movie->exists()) {
			throw new NotFoundException(__('Invalid movie'));
		}
		if ($this->Movie->delete()) {
			$this->Session->setFlash(__('Movie deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Movie was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	public function search(){
		if ($this->request->is('post')) {
			if (!empty($this->data)) {
				$sTitle = $this->data['Movie']['sTitle'];
				$sDirector = $this->data['Movie']['sDirector'];
				$sActor = $this->data['Movie']['sActor'];
				$sKeyword = $this->data['Movie']['sKeyword'];
				$sYear = $this->data['Movie']['sYear']['year'];
				/*if (strlen($sYear['year']) == 0):
					echo 'holaasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
				endif;*/
				
				$options['joins'] = array(
					array('table' => 'directors_movies',
						'alias' => 'DirectorsMovies',
						'type' => 'inner',
						'conditions' => array(
							'Movie.id = DirectorsMovies.movie_id'
						)
					),
					array('table' => 'directors',
						'alias' => 'Director',
						'type' => 'inner',
						'conditions' => array(
							'DirectorsMovies.director_id = Director.id'
						)
					),
					array('table' => 'actors_movies',
						'alias' => 'ActorsMovies',
						'type' => 'inner',
						'conditions' => array(
							'Movie.id = ActorsMovies.movie_id'
						)
					),
					array('table' => 'actors',
						'alias' => 'Actor',
						'type' => 'inner',
						'conditions' => array(
							'ActorsMovies.actor_id = Actor.id'
						)
					),
					array('table' => 'keywords_movies',
						'alias' => 'KeywordsMovies',
						'type' => 'inner',
						'conditions' => array(
							'Movie.id = KeywordsMovies.movie_id'
						)
					),
					array('table' => 'keywords',
						'alias' => 'Keyword',
						'type' => 'inner',
						'conditions' => array(
							'KeywordsMovies.keyword_id = Keyword.id'
						)
					)
				);

				$options['conditions'] = array(
					'Director.name LIKE' => "%$sDirector%",
					'Actor.name LIKE' => "%$sActor%",
					'Keyword.word LIKE' => "%$sKeyword%",
					'Movie.title LIKE' => "%$sTitle%",
					'ReleaseYear LIKE' => "%$sYear%"
				);
				
				$options['fields'] = array(
					'DISTINCT Movie.id', 'Movie.title', 'Movie.duration', 'strftime("%Y", release_date) AS "ReleaseYear"'
				);
			
				$this->set('movies', $this->Movie->find('all', $options));
			}
		}
	}
	
	public function searchRelated(){
			if (!empty($this->params['requested'])) {
			if ( isset($this->params['keyword'])){
				$keywords=$this->params['keyword'][0];
				$first= true;
				$condicion=NULL;
				
				foreach($keywords as $clave)
				{	
					if ($first){
						$condicion .= "( Keyword.word LIKE '%".$clave["word"]."%'";
						$first=false;
					}
					else
						$condicion .= " OR Keyword.word LIKE '%".$clave["word"]."%'";
				}
				$condicion .= ")";
				$movie=$this->params['movie']['0'];
				$options['joins'] = array(
					array('table' => 'keywords_movies',
						'alias' => 'KeywordsMovies',
						'type' => 'inner',
						'conditions' => array(
							'Movie.id = KeywordsMovies.movie_id'
						)
					),
					array('table' => 'keywords',
						'alias' => 'Keyword',
						'type' => 'inner',
						'conditions' => array(
							'KeywordsMovies.keyword_id = Keyword.id'
						)
					)
				);

				$options['conditions'] = array(
					$condicion,
					'Movie.title NOT LIKE' =>"%$movie%"
				);
				
				$options['fields'] = array(
					'DISTINCT Movie.id', 'Movie.title', 'Movie.duration', 'strftime("%Y", release_date) AS "ReleaseYear"'
				);
			
				return $this->Movie->find('all', $options);
			}
		}
	}
	
	public function searchWebService(){
		if (!empty($this->params['requested'])) {
			
			if ( isset($this->params['title'])){
				$title=$this->params['title'][0];
				$moviesWebService=IMDbScraper::search($title);
				
				$moviesWebService=array_slice($moviesWebService, 1, 6);
				$movies;
				$i=0;
				foreach($moviesWebService as $movie){
					$movies[$i]=IMDbScraper::info($movie[0]);
					$i++;
				}
				return $movies;
			}
		}
	}
	
}
